package io.renren.modules.canteens.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.canteens.dao.StockOrderDao;
import io.renren.modules.canteens.dao.StockOrdermxDao;
import io.renren.modules.canteens.dto.FoodDTO;
import io.renren.modules.canteens.dto.StockOrderDTO;
import io.renren.modules.canteens.dto.StockOrdermxDTO;
import io.renren.modules.canteens.dto.SupplierDTO;
import io.renren.modules.canteens.entity.StockOrdermxEntity;
import io.renren.modules.canteens.service.FoodService;
import io.renren.modules.canteens.service.StockOrderService;
import io.renren.modules.canteens.service.StockOrdermxService;
import io.renren.modules.canteens.service.SupplierService;
import io.renren.modules.sys.service.SysDeptService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 食材采购明细表
 *
 * @author 翟文闯 619556390@QQ.com
 * @since 1.0.0 2019-11-22
 */
@Service
public class StockOrdermxServiceImpl extends CrudServiceImpl<StockOrdermxDao, StockOrdermxEntity, StockOrdermxDTO> implements StockOrdermxService {
    @Autowired
    private StockOrdermxDao stockOrdermxDao;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private StockOrderService stockOrderService;
    @Autowired
    private SysDeptService sysDeptService;
    @Override
    public QueryWrapper<StockOrdermxEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<StockOrdermxEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public Map getOrderMx(Map<String, Object> params) {
        Map map = new HashMap<>();
        List<Map> list=new ArrayList<>();
        String stockOrderId =  (String)params.get("stockOrderId");
        if(StringUtils.isNotBlank(stockOrderId)){
            long stockOrderIdl = Long.parseLong(stockOrderId);
            List<StockOrdermxDTO> stockOrdermxList = stockOrdermxDao.getsupplierOrder(stockOrderIdl);
            if(stockOrdermxList.size()>0){
                for (StockOrdermxDTO ordermxDTO:stockOrdermxList) {
                    Map map1 = new HashMap<>();
                    SupplierDTO supplierDTO = supplierService.get(ordermxDTO.getSupplierId());
                    map1.put("SupplierName",supplierDTO.getSupplierName());
                    map1.put("orderMx",stockOrdermxDao.getOrderMx(stockOrderIdl, ordermxDTO.getSupplierId()));
                    map1.put("totalPrice",stockOrdermxDao.getTotalPrice(stockOrderIdl, ordermxDTO.getSupplierId()).getTotalPrice());
                    list.add(map1);
                }
                map.put("list",list);
                map.put("totalMony",stockOrdermxDao.getTotalMony(stockOrderIdl).getTotalPrice());

            }
        }


        return map;
    }

    @Override
    public List<StockOrdermxDTO> getOrderMx1( Map<String, Object> params) {
        String stockOrderId1 =  (String)params.get("stockOrderId");
        List<StockOrdermxDTO> list = new ArrayList<>();
        if(StringUtils.isNotBlank(stockOrderId1)){
            long stockOrderId = Long.parseLong(stockOrderId1);
            StockOrderDTO orderDTO = stockOrderService.get(stockOrderId);
            List<StockOrdermxDTO> stockOrdermxDTOList = stockOrdermxDao.getsupplierOrder(stockOrderId);
            for (StockOrdermxDTO ordermxDTO:stockOrdermxDTOList) {
                List<StockOrdermxDTO> orderMx = stockOrdermxDao.getOrderMx(stockOrderId, ordermxDTO.getSupplierId());
                for (StockOrdermxDTO ordermxDTO1:orderMx) {
                    ordermxDTO1.setTotalPrice1(String.valueOf(ordermxDTO1.getTotalPrice()));
                    list.add(ordermxDTO1);
                }
                StockOrdermxDTO totalPrice = stockOrdermxDao.getTotalPrice(stockOrderId, ordermxDTO.getSupplierId());
                StockOrdermxDTO ordermxDTO1 = new StockOrdermxDTO();
                ordermxDTO1.setMealTime(orderDTO.getMealTime());
                ordermxDTO1.setTotalPrice1("合计 ： "+ totalPrice.getTotalPrice());
                list.add(ordermxDTO1);
            }
            StockOrdermxDTO totalPrice = stockOrdermxDao.getTotalMony(orderDTO.getStockOrderId());
            StockOrdermxDTO ordermxDTO = new StockOrdermxDTO();
            ordermxDTO.setMealTime(orderDTO.getMealTime());
            ordermxDTO.setTotalPrice1("共计 ： "+totalPrice.getTotalPrice());
            list.add(ordermxDTO);

            String name = sysDeptService.get(orderDTO.getCanteenId()).getName();
            for (StockOrdermxDTO mxDTO:list) {
                if(StringUtils.isNotBlank(mxDTO.getSupplierName())){
                    BigDecimal unitPrice = new BigDecimal(mxDTO.getUnitPrice());
                    BigDecimal rrivalNum = new BigDecimal(mxDTO.getArrivalNum());
                    DecimalFormat df2 =new DecimalFormat("#.00");
                    mxDTO.setTotalPrice1(df2.format(unitPrice.multiply(rrivalNum)));
                    mxDTO.setCanteenName(name+"食材每日进货清单");
                }
            }
        }
        return list;
    }
}